﻿using System.Collections.Generic;

namespace CN2P.ServiceModel.ServiceClients
{
    using System;
    using System.Net;
    using System.Text;

    public class CartServiceClient : BaseServiceClient
    {

        public CartServiceClient(string baseUrl)
            : base(baseUrl)
        {
        }

        public void PutItem(string userId, string prodId, CartItemModel item)
        {
            PutItem(userId, prodId, Serialize(item));
        }

        public void PutItem(string userId, string prodId, string itemJson)
        {
            var itemsUrl = GetCartItemUrl(userId, prodId);
            var response = this.ExecutePut(itemsUrl, itemJson);
            ValidateResponse(response, HttpStatusCode.Created);
        }

        public void PostItem(string userId, CartItemModel item)
        {
            PostItem(userId, Serialize(item));
        }

        public void PostItem(string userId, string itemJson)
        {
            var itemsUrl = GetCartItemsUrl(userId);
            var response = ExecutePost(itemsUrl, itemJson);
            ValidateResponse(response, HttpStatusCode.Created);
        }

        public void DeleteItem(string userId, string prodId)
        {
            var itemUrl = GetCartItemUrl(userId, prodId);
            var response = ExecuteDelete(itemUrl);
            ValidateResponse(response, HttpStatusCode.OK);
        }

        public CartModel GetCart(string userId)
        {
            var url = GetCartUrl(userId);
            var response = ExecuteGet<CartModel>(url);
            if (response.Code == HttpStatusCode.OK)
                return response.Content;
            else if (response.Code == HttpStatusCode.NotFound)
                return new CartModel()
                    {
                        UserId = userId,
                        Items = new List<CartItemModel>()
                    };

            return null;
        }

        public CartModel GetCartOfAbsentUser(string userId)
        {
            var url = GetCartUrl(userId);
            var response = ExecuteGet<CartModel>(url);
            ValidateResponse(response, HttpStatusCode.NotFound);
            var model = response.Content;
            return model;
        }

        public void AddCart(string userId, CartModel cart)
        {
            AddCart(userId, Serialize(cart));
        }

        public void AddCart(string userId, string cartJson)
        {
            var url = GetCartUrl(userId);
            var response = ExecutePost(url, cartJson);
            ValidateResponse(response, HttpStatusCode.Created);
        }

        public void DeleteCart(string userId)
        {
            var url = GetCartUrl(userId);
            var response = ExecuteDelete(url);
            ValidateResponse(response, HttpStatusCode.OK);
        }

        public void DeleteAll()
        {
            var url = GetCartServiceUrl();
            var response = ExecuteDelete(url);
            ValidateResponse(response, HttpStatusCode.OK);
        }

        private string GetCartItemUrl(string userId, string prodId)
        {
            return this.GetCartItemsUrl(userId) + prodId + "/";
        }

        private string GetCartItemsUrl(string userId)
        {
            return this.GetCartUrl(userId) + "items/";
        }

        protected string GetCartUrl(string userId)
        {
            var bytes = Encoding.UTF8.GetBytes(userId);
            var base64 = Convert.ToBase64String(bytes);
            var encoded = Uri.EscapeUriString(base64);
            return GetCartServiceUrl() + encoded + "/";
        }

        private string GetCartServiceUrl()
        {
            return _baseUrl + "api/cart/";
        }
    }

}
